<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/registry.css">
</head>

<body>
    <div class="nav">网页聊天室</div>
    <div class="login-container">
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username" name="username">
            </div>

            <div class="row">
                <span>昵称</span>
                <input type="text" id="nickname" name="nickname">
            </div>

            <div class="row">
                <span>密码</span>
                <input type="password" id="password" name="password">
            </div>

            <div class="row">
                <span>再次输入密码</span>
                <input type="password" id="repeatedPassword" name="repeatedPassword">
            </div>

            <div class="row submit-row">
                <button id="submit">提交</button>
            </div>
        </div>
    </div>
</body>

<script src="js/jquery.min.js"></script>
<script src="js/common.js"></script>

<script src="js/jq-paginator.js"></script>
<script src="js/bootstrap.min.js"></script>

<script>
    let submitButton = document.querySelector('#submit');
    submitButton.onclick = function () {

        let username = document.querySelector('#username').value;
        let nickname = document.querySelector('#nickname').value;
        let password = document.querySelector('#password').value;
        let repeatedPassword = document.querySelector('#repeatedPassword').value;
        $.ajax({
            method: 'post',
            url: '/user/registry',
            data: {
                username: username,
                nickname: nickname,
                password: password,
                repeatedPassword: repeatedPassword
            },
            success: function (data) {
                console.log(JSON.stringify(data));
                if (data.code == 1002) {
                    alert("输入的参数不合法!")
                } else if (data.code == 1105) {
                    alert("两次输入的密码不一致!");
                } else if (data.code == 1101) {
                    alert("用户名已存在!");
                } else {
                    alert("注册成功!即将跳转到登录页面");
                    location.href = "/login.html";
                }
            }
        });
    }
</script>

</html>